package com.nanfang51g3.ckz.ui.money.withdraw;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.ui.base.IPresenter;
import com.nanfang51g3.ckz.ui.base.Base2Activity;
import com.nanfang51g3.ckz.ui.fragment.withdraw.WithdrawAliPayFragment;
import com.nanfang51g3.ckz.ui.fragment.withdraw.WithdrawBankFragment;
import com.nanfang51g3.ckz.ui.money.list.MoneyDetailActivity;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.SPUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/4/19.
 */
public class WithdrawActivity extends Base2Activity implements WithdrawContract.View, View.OnClickListener {
    WithdrawContract.WithdrawActionsListener withdrawActionsListener;
    @Bind(R.id.index_tv_right_title)
    TextView indexTvRightTitle;
    @Bind(R.id.rb_yes)
    RadioButton rbYes;
    @Bind(R.id.rb_no)
    RadioButton rbNo;
    @Bind(R.id.index_tv_title)
    TextView indexTvTitle;
    String deal_total;

    int type = 2;
    private FragmentManager fragmentManager;
    private Fragment currentFragment;
    private RadioButton[] rbs = new RadioButton[2];
    @Override
    public IPresenter getPresenter() {
        return null;
    }
    @Override
    public int getContentView() {
        return R.layout.withdraw;
    }

    @Override
    public boolean isShowLeftIcon() {
        return true;
    }

    @Override
    public boolean isShowRightIcon() {
        return false;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_accounting_left;
    }

    @Override
    public int setRightIcon() {
        return 0;
    }

    @Override
    public int showTitle() {
        return R.string.str_ti_xian_bank;
    }

    @Override
    public void onClickLeft(View v) {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void onClickRight(View v) {
        EasyToast.showShort(this, "msg");
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showMessage(String message) {
        EasyToast.showShort(this, message);
    }

    @Override
    public void finalView() {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void onIntent() {
        super.onIntent();
        deal_total = getIntent().getStringExtra("deal_total");
        KLog.i("Evd", "deal_total=" + deal_total);
        SPUtil.putAndApply(this
                , "deal_total", deal_total);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        withdrawActionsListener = DaggerWithdrawContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .withdrawPresenterModule(new WithdrawPresenterModule(this))
                .build().getWithdrawPresenter();
        KLog.i("TAG", withdrawActionsListener == null ? "withdrawActionsListener==null" : "withdrawActionsListener!=null");
        ButterKnife.bind(this);
        fragmentManager = this.getSupportFragmentManager();
        rbs[0] = rbYes;
        rbs[1] = rbNo;
        rbs[0].setOnClickListener(this);
        rbs[1].setOnClickListener(this);
        defaultInit();
        indexTvRightTitle.setVisibility(View.VISIBLE);
        indexTvRightTitle.setText("提现明细");

    }

    @OnClick(R.id.index_tv_right_title)
    public void index_tv_right_title(View view) {
        startActivity(new Intent(WithdrawActivity.this, MoneyDetailActivity.class).putExtra("id_type", type));
    }

    WithdrawBankFragment aFragment;
    WithdrawAliPayFragment bFragment;

    //　默认view
    public void defaultInit() {
        FragmentTransaction transaction = this.fragmentManager
                .beginTransaction();//获取Fragment事务
        aFragment = WithdrawBankFragment.newInstance(this, withdrawActionsListener);//创建Fragment
        transaction.add(R.id.content_frame, aFragment);//查找layout FrameLayout 替换fragment
        currentFragment = aFragment;
        transaction.commit();
    }

    @Override
    public void onClick(View view) {
        FragmentTransaction transaction = this.fragmentManager.beginTransaction();
        switch (view.getId()) {
            case R.id.rb_yes:
                if (aFragment == null) {
                    aFragment = WithdrawBankFragment.newInstance(this, withdrawActionsListener);//创建Fragment
                }
                switchContent(aFragment);
                indexTvTitle.setText("提现到银行卡");
                type = 2;
                break;

            case R.id.rb_no:
                if (bFragment == null) {
                    bFragment = WithdrawAliPayFragment.newInstance(this, withdrawActionsListener);//创建Fragment
                }
                switchContent(bFragment);
                indexTvTitle.setText("提现到支付宝");
                type = 1;
                break;
        }
        transaction.commit();
        // Replace whatever is in thefragment_container view with this fragment,
        // and add the transaction to the backstack
    }

    public void switchContent(Fragment fragment) {
        //记录第一个初始化Fragment不等于传进的的Fragment
        if (currentFragment != fragment) {
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            if (!fragment.isAdded()) {//传入的Fragment未添加过 隐藏当前添加新的Fragment
                transaction.hide(currentFragment).add(R.id.content_frame, fragment).commit();
            } else {
                transaction.hide(currentFragment).show(fragment).commit();//隐藏当前，显示传入的Fragment
            }
            currentFragment = fragment;//记录操作的fragment
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);//解除绑定，官方文档只对fragment做了解绑
    }
}
